package com.chao.yyks.util;

import com.chao.common.util.StrUtile;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 解析word
 */
public class ReadDocxExample {
    public static List<String> wordText(File file) {
        List<String> strs = new ArrayList<>();
        try (FileInputStream fis = new FileInputStream(file);
             XWPFDocument document = new XWPFDocument(fis)) {
            for (XWPFParagraph paragraph : document.getParagraphs()) {
                String s = paragraph.getText().replace(" ","").trim();
                String[] ss =  s.split("\n");
                for (String t:ss){
                    if(!StrUtile.isEmpty(t)){
                        strs.add(t);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return strs;
    }

    public static List<String> wordText(MultipartFile file) {
        List<String> strs = new ArrayList<>();
        try (InputStream fis = file.getInputStream();
             XWPFDocument document = new XWPFDocument(fis)) {
            for (XWPFParagraph paragraph : document.getParagraphs()) {
                String s = paragraph.getText().replace(" ","").trim();
                String[] ss =  s.split("\n");
                for (String t:ss){
                    if(!StrUtile.isEmpty(t)){
                        strs.add(t);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return strs;
    }
}